goog.require('goog.dom');
// goog.require('goog.ui.Button');
goog.require('goog.ui.Control');
 goog.require('goog.events');
goog.require('goog.ui.Dialog');

var player = null;

var MainPage = Class
		.create( {
			initialize : function() {
				// this.roomList;
				// this.userList;

			},
			init : function(o) {
				this.componentId = o.componentId;
				this.userName = this.getUrlParam('userName');

				if (this.roomListModel == null) {
					var init = {};
					this.roomListModel = ComponentManager.loadComponent( {
						componentId : 'roomListModel_instance',
						jsClassName : 'RoomListModel',
						initData : init
					});

					var roomListViewElements = {
						'targetLocation' : 'roomlist'
					};
					var init = {
						'mainComponentId' : this.componentId,
						'model' : this.roomListModel,
						'controller' : '',
						'elements' : roomListViewElements
					};
					this.roomListView = ComponentManager.loadComponent( {
						componentId : 'roomListView_instance',
						jsClassName : 'RoomListView',
						initData : init
					});
				}
				this.roomListView.show();

				if (this.userListModel == null) {
					var init = {};
					this.userListModel = ComponentManager.loadComponent( {
						componentId : 'userListModel_instance',
						jsClassName : 'UserListModel',
						initData : init
					});

					var userListViewElements = {
						'targetLocation' : 'userlist'
					};
					var init = {
						'model' : this.userListModel,
						'controller' : '',
						'elements' : userListViewElements
					};
					this.userListView = ComponentManager.loadComponent( {
						componentId : 'userListView_instance',
						jsClassName : 'UserListView',
						initData : init
					});
				}
				this.userListView.show();

				clientSideInclude('deskfarme', 'gameWindow.html');

				if (this.chatMsgModel == null) {
					var init = {};
					this.chatMsgModel = ComponentManager.loadComponent( {
						componentId : 'chatMsgListModel_instance',
						jsClassName : 'ChatMsgListModel',
						initData : init
					});

					var init = {
						'model' : this.chatMsgModel
					};
					this.chatWidgetController = ComponentManager
							.loadComponent( {
								componentId : 'chatWidgetController_instance',
								jsClassName : 'ChatWidgetController',
								initData : init
							});

					var chatElements = {};
					chatElements.txtMsg = goog.dom.getElement('txtMsg');
					chatElements.txtMsgArea = goog.dom.getElement('txtMsgArea');
					chatElements.btnSend = goog.dom.getElement('send');

					// var roomListViewElements = {'targetLocation':''};
					var init = {
						'parentComponentId' : this.componentId,
						'model' : this.chatMsgModel,
						'controller' : this.chatWidgetController,
						'elements' : chatElements
					};
					this.chatWidgetView = ComponentManager.loadComponent( {
						componentId : 'chatWidgetView_instance',
						jsClassName : 'ChatWidgetView',
						initData : init
					});
				}
				this.chatWidgetView.show();
				this.initLoginPage();

				dwr.engine.setActiveReverseAjax(true);
				this.componentId = o.componentId;
				// this.roomList = [];
				// this.userList = [];
				// HostServiceCenter.initMainPage(this.componentId);

				this.version = 0;
			},
			updateDataVersion : function(version) {
				this.version = version;
			},
			getDataVersion : function(version) {
				return this.version;
			},
			errorMsgShow : function(error) {
				alert(error);
			},

			initLoginPage : function() {
				HostServiceCenter.onLoad();
			},

			showLoginPanel : function(user) {
				if (user.state == -1) {
					clientSideInclude('linkPanel', 'login.html');
					this.disbaled = " disabled ";
				} else {
					var userInfoSpan = goog.dom.createDom('a', {
						'style' : 'float:right;padding:5px',
						'href' : 'javascript:{}',
						'value' : 'logout',
						'id':'userName',
						'onclick' : function() {
							componentExecFunction('mainframe_instance',
									'changeNickName', null);
						}
					}, user.name + " | ");
					// this.btnLogout = new goog.ui.Button('logout');
					// this.lnkLogout = goog.dom.createDom('a',
					// {'href':'','id':'logout123'},'logout');
					// var lnkLogout = goog.dom.createDom('a', {'href':
					// 'javascript:{}','style':
					// 'float:right;padding:5px','onclick':alert("sss")},'logout');
					var lnkLogout = goog.dom.createDom('a', {
						'style' : 'float:right;padding:5px',
						'href' : 'javascript:{}',
						'value' : 'logout',
						'onclick' : function() {
							componentExecFunction('mainframe_instance',
									'logout', null);
						}
					}, 'logout');
					var logoutSpan = goog.dom.createDom('span', {
						'id' : DomConstants.SPN_LOGOUT
					}, lnkLogout);
					this.linkPanel = goog.dom
							.createDom(
									'div',
									{
										'style' : 'float:right;-moz-border-radius:5px;border:1px solid white;width:250px;height:30px;padding:3px;background-color:#75684D;color:black;font-size:18px',
										'id' : DomConstants.HEAEDR_LINK_PANEL
									}, logoutSpan, userInfoSpan);
					// goog.dom.appendChild(document.body, linkPanel);
					this.lnkCnt = new goog.ui.Control(this.linkPanel);
					this.lnkCnt.addClassName("dashboard-content");
					this.lnkCnt.addClassName("goog-inline-block");
					this.lnkCnt.render(goog.dom.$('linkPanel'));
				}
				HostServiceCenter.initMainPage(this.componentId);
				//setInterval("checkForUpdate()",15000);
			},
			
			changeNickName : function(){
				alert('change');
				var dialog1 = new goog.ui.Dialog();
				var txtNewNick = goog.dom.createDom('input', {
					'type' : 'test',
					'id' : 'txtNewNickName'
//					'value' : 'logout',
//					'onclick' : function() {
//						componentExecFunction('mainframe_instance',
//								'changeNickName', null);
//					}
				}, "");
				dialog1.setContent('Enter new Nick <input type="text" id="txtNickName"/>');
//				dialog1.setContent('<img src="http://images.icanhascheezburger.com/' +
//				        'completestore/2009/3/25/128825075025577352.jpg" ' +
//				        'width="400" height="255"><br>' +
//				        'Lorem ipsum dolor sit amet, consectetuer' +
//				        'adipiscing elit. Aenean sollicitudin ultrices urna. Proin vehicula ' +
//				        'mauris ac est. Ut scelerisque, risus ut facilisis dictum, est massa ' +
//				        'lacinia lorem, in fermentum purus ligula quis nunc. Duis porttitor ' +
//				        'euismod risus. Nam hendrerit lacus vehicula augue. Duis ante.');

			    dialog1.setTitle('My favorite LOLCat');

			    dialog1.setButtonSet(goog.ui.Dialog.ButtonSet.CONTINUE_SAVE_CANCEL);

			    goog.events.listen(dialog1, goog.ui.Dialog.EventType.SELECT, function(e) {
			      //alert('You chose: ' + e.key);
			    	componentExecFunction('mainframe_instance',
							'saveNick', null);
			    });
			    goog.events.listen(window, 'unload', function() {
			        goog.events.removeAll();
			      });

			    dialog1.setVisible(true);


			},
			
			saveNick:function(){
				var txtNickName = $('txtNickName');
				alert("New Nick : "+txtNickName.value);
				if(txtNickName == null){
					alert('Please enter nick name value');
					return;
				}
				HostServiceCenter.changeUserProfile(txtNickName.value);
			},

			initMainPage : function(data) {
				// alert('init main page');
				this.roomListView.setVersionNo(this.version);
				this.roomListModel.updateList(data['roomlist']);
				this.userListModel.updateList(data['userlist']);
				if (!this.isJoinedRoom) {
					this.chatMsgModel.inititalizeList(data['msglist']);
				}

			},

			// quitFromRoom(data){
			//		
			// },

			showGameDesk : function(data) {
				this.isJoinedRoom = true;
				clientSideInclude('deskfarme', 'deskFrame.html');
				var init = {
					id : "room_instance",
					'user' : data['user']
				};

				this.room = ComponentManager.loadComponent( {
					componentId : 'room_instance',
					jsClassName : 'Room',
					initData : init
				});
				player = ComponentManager
						.getComponentById('current_player_instance');
				RoomService.initRoom('room_instance');
				this.chatMsgModel.inititalizeList(data['MSGLIST']);
			},

			showUserHistoryWindow : function(historyList) {
				alert('user history');
				if (historyList != null) {
					alert('user history size : ' + historyList.length);
				}
			},

			logout : function() {
				// alert('aaaa');
				HostServiceCenter.logout("mainframe_instance");
			},

			afterLogout : function() {
				window.location = 'insightgoogle/logout';
			},
			
			updateSelfProfileInfo:function(user){
				var userName = $('userName');
				userName.innerHTML = user.name;
			},
			

			getUrlParam : function(paramName) {
				var paramName = paramName.replace(/[\[]/, "\\\[").replace(
						/[\]]/, "\\\]");
				var regexS = "[\\?&]" + paramName + "=([^&#]*)";
				var regex = new RegExp(regexS);
				var results = regex.exec(window.location.href);
				if (results == null)
					return "";
				else
					return results[1];
			}

		});